package com.ycb.concurrency.limitation;

import com.ycb.concurrency.limitation.rate.RateLimiter;
import com.ycb.concurrency.limitation.rate.SlidingTimeRateLimiter;
import lombok.extern.slf4j.Slf4j;

/**
 * @author 黄泽群
 * @since 2024-01-03
 */
@Slf4j
public class RateLimiterApp {

    public static void main(String[] args) throws InterruptedException {
        log.info("...initialization...");
        // 固定时间速率限制器
//        RateLimiter rateLimiter1 = new FixedTimeRateLimiter(5, 1000);
//        for (int i = 0 ; i < 20 ; i++) {
//            rateLimiter1.tryAcquire();
//            Thread.sleep(80);
//        }
        // 滑动时间窗口限流
        RateLimiter rateLimiter2 = new SlidingTimeRateLimiter(2, 500, 10);
        for (int i = 0 ; i < 20 ; i++) {
            rateLimiter2.tryAcquire();
            Thread.sleep(80);
        }
    }

}
